import { emoji, ruleStr, emojiReplace } from './../../../assets/emoticon';
import { mapState, mapMutations } from 'vuex';
export default {
  name: 'customerList',
  data() {
    return {
      isRead: null,
      userClass: 0,
      selectObj: {},//选中的用户列表
      dataList: [],

      currentPage: 1,//当前页面
      pageSize: 20,//分页大小
      total: 0,
      number: 1,
      numberEnd: 10,
      loading: true
    }
  },
  components: {
    retime: () => { return import('../../../components/momentRenameTime.vue') },
  },
  computed: {
    ...mapState([
      'isMobile', 'wsMsgFromApp'
    ]),
  },
  watch: {
    // socket消息
    '$store.state.wsMsgFromApp': {
      deep: true,
      handler: function (nv, ov) {
        // console.log(nv)
        this.getWSMsg(nv)
      }
    },
  },
  methods: {
    // pageSize 改变时会触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppCustomerList()
    },

    // 点击页码时改变会触发/点击上一页触发/点击下一页触发
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAppCustomerList()
    },

    // 对文件类型进行处理
    JSON2HTML: function (item) {
      if (item.type == 1) { return `<b>【图片】</b>`; }
      if (!Boolean(item.msgBody)) { return ''; }
      return item.msgBody.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/\\n/g, '<br>');

      // return emojiReplace(item.msgBody)
    },

    // 已读未读筛选
    setConditional(event, key, value) {
      if (key == 'isRead') {
        this.isRead = (value == this.isRead) ? null : value
      }
      if (key == 'userClass') {
        this.userClass = (value == this.userClass) ? 0 : value
      }
      let target = event.target;
      if (target.nodeName == "SPAN") {
        target = event.target.parentNode;
      }
      target.blur();
      this.getAppCustomerList()
    },

    // 单条数据选中
    clickSingleItem(i, v) {

      this.selectObj = v
      this.$emit('getUserMsg', v)
      this.updateRead(v.lastMsgId, i)
    },

    // 获取socket的数据，增加未读的小红点
    getWSMsg(msg) {
      let idx = this.dataList.findIndex((v, i) => v.userId == msg.usersId)
      if (idx < 0) {//不在当前列表中,插入第一
        this.dataList.unshift({
          imgUrl: msg.imgUrl,
          nicheng: msg.nicheng,
          name: msg.name,//客户姓名，估计是敏感信息，不过与我何关
          salesmanName: msg.salesmanName,//业务员姓名
          userId: msg.usersId,
          msgBody: msg.msgBody,
          type: msg.type,
          lastMsgId: msg.msgId,
          lastMsgTime: msg.createTimeDate,
          imgW: msg.imgW,
          imgH: msg.imgH,
          addTime: msg.addTime,
          isRead: false
        })
      } else {//在当前列表中,移为第一
        let t = this.dataList.splice(idx, 1)[0];
        this.dataList.unshift(t);
        this.dataList[0].type = msg.type;
        this.dataList[0].msgBody = msg.msgBody;
        this.dataList[0].addTime = msg.addTime;
        if (this.selectObj.userId == msg.usersId) {
          this.dataList[0].isRead = true
        } else {
          this.dataList[0].isRead = false
        }
      };
    },

    // 获取列表数据
    getAppCustomerList() {
      this.loading = true;
      this.number = (this.currentPage - 1) * this.pageSize + 1;
      this.numberEnd = this.number + this.pageSize - 1
      let params = {
        companyEmployeeId: this.$store.getters.getCEID(false).companyEmployeeId,
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
        isRead: this.isRead,
        userClass: this.userClass,//【0：未指定，1：注册，2：初级，3：高级】
      }
      this.$api.getAppCustomerList(params).then(res => {
        this.loading = false
        if (res.data.code == 200) {
          if (res.data.data) {
            this.total = res.data.data.count
            this.dataList = res.data.data.customerList
          } else {
            this.total = 0
            this.dataList = []
          }
        }
      }).catch(err => {
        this.$message({
          message: err.message,
          type: 'error',
          duration: 3000,
        });
      })
    },

    // 点击列表将消息变为已读
    updateRead(lastMsgId, index) {
      let params = {
        lastMsgId: lastMsgId
      }
      this.$api.updateUnRead(params).then(res => {
        this.$set(this.dataList[index], 'isRead', true)
        // this.getAppCustomerList()
      }).catch(err => {
        this.$message({
          message: err.message,
          type: 'error',
          duration: 3000,
        });
      })
    }
  },
  mounted() {
    this.getAppCustomerList()
  },
}